package com.xuetang9.fujian.socketdemo.tcpdemo.onetoone;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author: fujian
 * @description:
 * @date: 2021/5/10 8:38
 * @version: V1.0
 * @package: com.xuetang9.fujian.socketdemo.tcpdemo.onetoone
 **/
public class Client {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        Socket socket = new Socket("localhost",9527);
        System.out.println("已成功连接到服务器");
        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();
        ObjectOutputStream outStream = new ObjectOutputStream(outputStream);
        ObjectInputStream inStream = new ObjectInputStream(inputStream);

        while (true){
            System.out.println("请输入要发送的消息");
            String message = scanner.next();
            if ("bye".equals(message)) break;
            outStream.writeObject(message);
            outStream.flush();
            Object receive = inStream.readObject();
            System.out.println("成功接收到来自服务端的消息：" + receive);
        }
        System.out.println("客户端已断开连接");


    }
}
